package com.team.cluedo.app;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Gallery;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.team.cluedo.Game;
import com.team.cluedo.R;
import com.team.cluedo.app.utils.CluedoMenu;
import com.team.cluedo.app.utils.ImageAdaptater;
import com.team.cluedo.app.waitscreens.WaitForYourTurn;

public class ChoosePerso extends Activity {
	static int index;
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		boolean res = CluedoMenu.onCreateOptionsMenu(menu, this);
		menu.getItem(CluedoMenu.MENU_CURRENT_GAME).setEnabled(false);
		menu.getItem(CluedoMenu.MENU_MY_CARDS).setEnabled(false);
		return res;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		return CluedoMenu.onOptionsItemSelected(item, this);
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
	    setContentView(R.layout.chooseperso);

	    Gallery g = (Gallery) findViewById(R.id.gallery);
	    g.setAdapter(new ImageAdaptater(this,Game.cluedo.nameOfCharacter));

	    g.setOnItemClickListener(new OnItemClickListener() {
	        public void onItemClick(AdapterView parent, View v, int position, long id) {
	        	index = position;
	            Toast.makeText(ChoosePerso.this, Game.cluedo.getNameOfCharacter(position), Toast.LENGTH_SHORT).show();
	        }
	    });

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent msg) {
		if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
			Game.cluedo.chooseCharacter(index);
			Intent defineIntent = new Intent(this,WaitForYourTurn.class);
			Game.cluedo.activity.startActivity(defineIntent);
	     	this.finish();
		}
	return false;
	}

}
